package com.dream.common.dao.search;




import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.stereotype.Repository;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.dream.common.bean.User;
import com.dream.common.bean.page.PageBean;
import com.dream.common.bean.page.PageList;
import com.dream.common.bean.page.Pages;
import com.dream.common.dao.base.search.AbstractSearchDao;

@Repository
public class UserSearchDao extends AbstractSearchDao< User , Long >
{
	
	@Override
	protected Class< User > getClassType ( )
	{
		return User.class;
	}
	
	@SuppressWarnings ( "deprecation" )
	public PageBean goodsFullSearchPage ( String [ ] fieldNames , String sql , PageBean pageBean )
	{
		Analyzer analyzer = new IKAnalyzer ( );
		FullTextSession fullTextSession = Search.getFullTextSession ( this.getSession ( ) );
		QueryParser parser = new MultiFieldQueryParser ( fieldNames , analyzer );
		org.apache.lucene.search.Query luceneQuery;
		try
		{
			luceneQuery = parser.parse ( sql );
		}
		catch ( ParseException e )
		{
			e.printStackTrace ( );
			return pageBean;
		}
		FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery ( luceneQuery , getClassType ( ) );
		int totalsize = fullTextQuery.getResultSize ( );
		pageBean.setTotalRows ( totalsize );
		fullTextQuery.setFirstResult ( pageBean.getOffset ( ) );
		fullTextQuery.setMaxResults ( pageBean.getRowsPerPage ( ) );
		List resultList = fullTextQuery.list ( );
		List list = new ArrayList ( );
		SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter ( "<strong class=\"cYellow\">" , "</strong>" );
		Highlighter highlight = new Highlighter ( simpleHTMLFormatter , new QueryScorer ( luceneQuery ) );
		highlight.setTextFragmenter ( new SimpleFragmenter ( 300 ) );
		for ( Object result : resultList )
		{
			User doc = ( User ) result;
			String nickName = doc.getName ( );
			nickName = highlightField ( highlight , analyzer , "name" , nickName );
			doc.setName ( nickName );
			
			list.add ( doc );
		}
		pageBean.setResultSetList ( list );
		return pageBean;
	}
	
	public PageList fullSearchPage ( String key , Pages pages )
	{
		List resultList = null;
		PageList pageList = new PageList ( );
		List list = new ArrayList ( );
		String [ ] fieldNames =
		{ "title" , "introduce" , "content" };
		// Analyzer analyzer = new MaxWordAnalyzer();
		Analyzer analyzer = new IKAnalyzer ( );
		try
		{
			FullTextSession fullTextSession = Search.getFullTextSession ( this.getSession ( ) );
			QueryParser parser = new MultiFieldQueryParser ( fieldNames , analyzer );
			String sql = "isAllowSearch:1 AND searchKind:user AND (title:" + key + " OR introduce:" + key + " OR content:" + key + ")";
			org.apache.lucene.search.Query luceneQuery = parser.parse ( sql );
			FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery ( luceneQuery , getClassType ( ) );
			int totalsize = fullTextQuery.getResultSize ( );
			pages.setTotalNum ( totalsize );
			pages.executeCount ( );
			fullTextQuery.setFirstResult ( pages.getSpage ( ) );
			fullTextQuery.setMaxResults ( pages.getPerPageNum ( ) );
			resultList = fullTextQuery.list ( );
			SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter ( "<span class=\"cRed\">" , "</span>" );
			Highlighter highlight = new Highlighter ( simpleHTMLFormatter , new QueryScorer ( luceneQuery ) );
			highlight.setTextFragmenter ( new SimpleFragmenter ( 300 ) );
			for ( Object result : resultList )
			{
				User doc = ( User ) result;
				String nickName = doc.getName ( );
				nickName = highlightField ( highlight , analyzer , "title" , nickName );
				doc.setName ( nickName );
				
				list.add ( doc );
			}
			pageList.setObjectList ( resultList );
			pageList.setPages ( pages );
			return pageList;
		}
		catch ( ParseException e )
		{
			e.printStackTrace ( );
			return null;
		}
	}
	
	public List< User > findListForIndex ( int firstResult , int maxResults )
	{
		String hql = "from FictionUser ";
		return this.findByQueryStringValues ( hql , null , firstResult , maxResults );
	}
	
	public void listToIndex ( List< User > list )
	{
		if ( list == null || list.size ( ) < 1 || list.isEmpty ( ) )
			return;
		FullTextSession fullTextSession = Search.getFullTextSession ( getSession ( ) );
		for ( User fictionUser : list )
		{
			// if (goods.getStatus() != 1)
			// continue;
			// goods.setIsAllowSearch(1);
			// user.setSearchKind("user");
			fullTextSession.index ( fictionUser );
		}
		fullTextSession.flushToIndexes ( );
		fullTextSession.clear ( );
	}
	
}
